package service
{{$ViewName := .Name}}
import (
	"context"
	"github.com/liuxd6825/dapr-go-ddd-sdk/errors"
	"github.com/liuxd6825/dapr-go-ddd-sdk/utils/randomutils"
    "github.com/liuxd6825/dapr-go-ddd-sdk/utils/timeutils"
	"{{.Namespace}}/pkg/query-service/application/internals/{{.aggregate_name}}/appquery"
	"{{.Namespace}}/pkg/query-service/domain/{{.aggregate_name}}/view"
	"{{.Namespace}}/pkg/query-service/infrastructure/test"
	"{{.Namespace}}/pkg/query-service/infrastructure/utils"
	"testing"
	"time"
)

func Test{{.Name}}QueryService(t *testing.T) {
	test.InitQuery()

	service := Get{{.Name}}AppQueryService()

	ctx := context.Background()
	tenantId := test.TenantId
	id := randomutils.UUID()
	t.Logf("id = %v", id)
	var vList []*view.{{.Name}}View

	t.Run("Create", func(t *testing.T) {
		v := &view.{{.Name}}View{}
        {{- range $name, $property := .AllViewProperties}}
        {{- if not $property.IsArray }}
        v.{{$name}} = {{$property.GoRepositoryTestValue}}
        {{- end }}
        {{- end }}

		if err := utils.SetViewDefaultFields(ctx, v, timeutils.Now(), utils.SetViewCreated); err != nil {
			t.Error(err)
		}
		if err := test.PrintJson(t, "Create", v); err != nil {
			t.Error(err)
		}
		if err := service.Create(ctx, v); err != nil {
			t.Error(err)
		}
		if findView, ok ,err := service.FindById(ctx, tenantId, id); err!=nil {
			t.Error(err)
		} else if ok {
			if err := findView.Equal(v); err!=nil {
				_ = test.PrintJson(t, "find equal", findView)
				t.Error(err)
			}
		}
	})

	t.Run("Update", func(t *testing.T) {
		v := &view.{{.Name}}View{}
        {{- range $name, $property := .AllViewProperties}}
            {{- if not $property.IsArray }}
        v.{{$name}} = {{$property.GoRepositoryTestValue}}
            {{- end }}
        {{- end }}

		if err := utils.SetViewDefaultFields(ctx, v, timeutils.Now(), utils.SetViewUpdated); err != nil {
			t.Error(err)
		}
		if err := test.PrintJson(t, "Update", v); err != nil {
			t.Error(err)
		}
		if err := service.Update(ctx, v); err != nil {
			t.Error(err)
		}

        if findView, ok, err:= service.FindById(ctx, tenantId, id); err!=nil {
            t.Error(err)
        } else if ok {
            if err := findView.Equal(v); err!=nil {
                _ = test.PrintJson(t, "find equal", findView)
                t.Error(err)
            }
        }
	})

	t.Run("CreateMany", func(t *testing.T) {
		dateTime := timeutils.Now()
		for i := 0; i < 10; i++ {
			v := &view.{{.Name}}View{}
			id := randomutils.UUID()
            {{- range $name, $property := .AllViewProperties}}
                {{- if not $property.IsArray }}
            v.{{.Name}} = {{$property.GoRepositoryTestValue}}
                {{- end }}
            {{- end }}
			if err := utils.SetViewDefaultFields(ctx, v, dateTime, utils.SetViewCreated); err != nil {
				t.Error(err)
			}
			vList = append(vList, v)
		}

		if err := test.PrintJson(t, "CreateMany", vList); err != nil {
			t.Error(err)
		}
		if err := service.CreateMany(ctx, vList); err != nil {
			t.Error(err)
		}
	})

	t.Run("UpdateMany", func(t *testing.T) {
		for _, v := range vList {
            {{- range $name, $property := .AllViewProperties}}
                {{- if not $property.IsArray }}
                    {{- if not $property.NameIsId}}
            v.{{$name}} = {{$property.GoRepositoryTestValue}}
                    {{- end }}
                {{- end }}
            {{- end }}
			if err := utils.SetViewDefaultFields(ctx, v, time.Now(), utils.SetViewUpdated); err != nil {
				t.Error(err)
			}
		}
		if err := test.PrintJson(t, "UpdateMany", vList); err != nil {
			t.Error(err)
		}
		if err := service.UpdateMany(ctx, vList); err != nil {
			t.Error(err)
		}
	})

	t.Run("FindById", func(t *testing.T) {
		if v, ok, err := service.FindById(ctx, tenantId, id); err != nil {
			t.Error(err)
		} else if ok {
            if err := test.PrintJson(t, "FindById", v); err != nil {
                t.Error(err)
            }
		} else {
			t.Error(errors.New("not found"))
		}
	})

	t.Run("FindByIds", func(t *testing.T) {
		var ids []string
		for _, v := range vList {
			ids = append(ids, v.Id)
		}

		if list, ok, err := service.FindByIds(ctx, tenantId, ids); err != nil {
			t.Error(err)
		} else if ok {
			if err := test.PrintJson(t, "FindByIds", list); err != nil {
				t.Error(err)
			}
		} else {
			t.Error(errors.New("not found"))
		}
	})

	t.Run("FindPaging", func(t *testing.T) {
		aq := &appquery.{{.Name}}FindPagingAppQuery{
			TenantId: tenantId,
			PageNum:  0,
			PageSize: 10,
			Filter:   "",
			Sort:     "",
			Fields:   "",
            IsTotalRows: true,
		}
		if pagingResult, ok, err := service.FindPaging(ctx, aq); err != nil {
			t.Error(err)
		} else if ok {
			if err := test.PrintJson(t, "FindPaging", pagingResult); err != nil {
				t.Error(err)
			}
		} else {
			t.Error(errors.New("not found"))
		}
	})

    t.Run("FindAll", func(t *testing.T) {
        if list, ok, err := service.FindAll(ctx, tenantId); err != nil {
            t.Error(err)
        } else if ok {
            if err := test.PrintJson(t, "FindAll", list); err != nil {
                t.Error(err)
            }
        } else {
            t.Error(errors.New("not found"))
        }
    })

	t.Run("DeleteById", func(t *testing.T) {
		t.Logf("id = %v", id)
		if err := service.DeleteById(ctx, tenantId, id); err != nil {
			t.Error(err)
		}

		if v, ok, err := service.FindById(ctx, tenantId, id); err != nil {
            t.Error(err)
        } else if ok {
            t.Error(errors.New("delete failed"))
            if err := test.PrintJson(t, "DeleteById", v); err != nil {
                t.Error(err)
            }
        }
	})

	t.Run("DeleteMany", func(t *testing.T) {
		dList := vList[0:2]
		vList = vList[3:]
		if err := test.PrintJson(t, "DeleteMany", dList); err != nil {
			t.Error(err)
		}
		if err := service.DeleteMany(ctx, tenantId, dList); err != nil {
			t.Error(err)
		}

        var ids []string
        for _, v := range dList {
            ids = append(ids, v.Id)
        }

        if list, ok, err := service.FindByIds(ctx, tenantId, ids); err != nil {
            t.Error(err)
        } else if ok {
            t.Error(errors.New("delete failed"))
			if err := test.PrintJson(t, "DeleteMany", list); err != nil {
				t.Error(err)
			}
        }
	})

    {{- if .IsEntity }}
	t.Run("DeleteBy{{.AggregateName}}Id", func(t *testing.T) {
		{{.aggregate_name}}Id := "{{.aggregate_name}}Id"
		if err := service.DeleteBy{{.AggregateName}}Id(ctx, tenantId, {{.aggregate_name}}Id); err != nil {
			t.Error(err)
		}

        if list, ok, err := service.FindBy{{.AggregateName}}Id(ctx, tenantId, {{.aggregate_name}}Id); err != nil {
            t.Error(err)
        } else if ok {
            t.Error(errors.New("delete failed"))
			if err := test.PrintJson(t, "DeleteBy{{.AggregateName}}Id", list); err != nil {
				t.Error(err)
			}
        }
	})
	{{- end }}

	t.Run("DeleteAll", func(t *testing.T) {
		if err := service.DeleteAll(ctx, tenantId); err != nil {
			t.Error(err)
		}
        if list, ok, err := service.FindAll(ctx, tenantId); err != nil {
            t.Error(err)
        } else if ok {
            t.Error(errors.New("delete failed"))
            if err := test.PrintJson(t, "DeleteAll", list); err != nil {
                t.Error(err)
            }
        }
	})

}
